import React, { Component } from 'react'
import PureRenderMixin from 'react-addons-pure-render-mixin'

import * as homeData from '../../../fetch/home/home'
import HomeAd from '../../../components/HomeAd'


class Ad extends Component{
  constructor(props){
    super(props);
    this.shouldComponentUpdate = PureRenderMixin.shouldComponentUpdate.bind(this);
    this.state = {
      data: []
    }
  }

  render() {
    return (
      <div>
        {
          !this.state.data.length
          ? <p>加载中...</p>
          : <HomeAd data={this.state.data}/>
        }
      </div>
    )
  }

  componentDidMount() {
    const result = homeData.getAdData();
    result.then(res => {
      return res.json();
    }).then(json => {
      const data = json;
      if(data.length) {
        this.setState({
          data: data
        })
      }
    }).catch(ex => {
      if(__DEV__) {
        console.error('home页获取数据失败，出现异常');
      }
    });
  }
}

export default Ad;
